<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\response\Json;

class Page extends Base{

    public function index()
    {
        $where = [];
        $list = db('page')->where($where)->order('id ase')->paginate(2);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function create(){
        return $this->fetch();
    }
    public function save(){
        if (request()->isPost()){
            $inputdata = input();
//          dump($inputdata);exit();
            unset($inputdata['file']);
            //$articlemodel = model("Fodder");
            $res=db("page")->insert($inputdata);
//             dump($inputdata);exit();
            // $res = $articlemodel->add($inputdata);

            if ($res){
                $this->success("添加成功","page/index");
            }else{
                $this->error("添加失败");
            }
        }
        return $this->fetch();
    }
    public function edit(){

        $id = input('id');
        $find = db('page')->where(['id'=>$id])->find();
        $this->assign("find",$find);
        return $this->fetch();
    }
    public function update(){
        if(request()->isPost){
            $inputdata=input();
            $id=input('id');
            unset($inputdata['file']);
            $res = db("page")->where(['id'=>$id])->update($inputdata);
            if ($res){
                $this->success('修改成功','page/index');
            }else{
                $this->error('修改失败');
            }
        }
    }
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move('./upload/');
        if($info){
            $image = \think\Image::open($info);
            // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
            $image->thumb(150,150)->save("./upload/".$info->getSaveName());
            $path = 'http://jiaoyu.com/upload/'.$info->getSaveName();
            return ["code"=>"0","msg"=>"上传成功","data"=>$path];
        }else{
            return ["code"=>"1","msg"=>"失败","data"=>""];
        }
    }
    public function remove(){
        $id =input('id');
        $res = db("page")->where(["id"=>$id])->delete();
        if ($res){
            $this->success("删除成功","page/index");
        }else{
            $this->error("删除失败");
        }
    }
}